package fhv.itb.ec.book;

import java.io.File;
import java.net.MalformedURLException;
import java.rmi.MarshalledObject;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.activation.Activatable;
import java.rmi.activation.ActivationDesc;
import java.rmi.activation.ActivationException;
import java.rmi.activation.ActivationGroup;
import java.rmi.activation.ActivationGroupDesc;
import java.rmi.activation.ActivationGroupID;
import java.rmi.activation.UnknownGroupException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;


public class BookActivator {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		
		try {
			
			System.out.println("Aktivierungsdeskriptoren konstruieren...");
			Properties props = new Properties();
			props.put("java.security.policy", new File("server.policy").getCanonicalPath());
			ActivationGroupDesc group = new ActivationGroupDesc(props, null);
			ActivationGroupID id = ActivationGroup.getSystem().registerGroup(group);
			MarshalledObject param = new MarshalledObject("SuperBook");
			
			System.out.println("Get Url...");
			//String classDir = ".";
			//String classURL = new File(classDir).getCanonicalFile().toURL().toString();
			
			System.setSecurityManager(new RMISecurityManager());
			
			String codebase = "http://192.168.1.33:8080/Download/";
			System.setProperty("java.rmi.server.codebase", codebase);
			
			ActivationDesc desc1 = new ActivationDesc(id, BookImpl.class.getName(), codebase, param);
			BookServer bs1 = (BookServer)Activatable.register(desc1);
			
			System.out.println("Aktivierbare Implementierungen an Registrierung binden.....");
			
			//Naming.rebind("BookServer", bs1);
			
			Context namingContext = new InitialContext();
			namingContext.bind("rmi:BookServer", bs1);
			
			System.out.println("beenden...");
			
		} catch (Exception e) {
			e.printStackTrace();
		}


	}

}
